package org.looom.orglooomschool;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity14 extends AppCompatActivity {

    // 定义我们自定义的 Action 字符串，注意替换包名
    private static final String CUSTOM_SMS_RECEIVED_ACTION = "com.example.myapplication.ACTION_NEW_SMS_RECEIVED";
    private MyCustomReceiver14 customReceiver;
    private IntentFilter intentFilter;
    private TextView smsStatusTextView;
    private Button sendBroadcastButton;

    @SuppressLint("UnspecifiedRegisterReceiverFlag")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity14);

        smsStatusTextView = findViewById(R.id.smsStatusTextView);
        sendBroadcastButton = findViewById(R.id.sendBroadcastButton);

        // --- 动态注册自定义广播接收者 ---
        // 实例化自定义广播接收者，并将 TextView 传递进去
        customReceiver = new MyCustomReceiver14(smsStatusTextView);
        // 实例化过滤器
        intentFilter = new IntentFilter();
        // 添加要监听的自定义 Action
        intentFilter.addAction(CUSTOM_SMS_RECEIVED_ACTION);

        // 动态注册广播接收者
        registerReceiver(customReceiver, intentFilter, Context.RECEIVER_EXPORTED);

        // --- 注册结束 ---

        // --- 设置按钮点击监听器，发送自定义广播 ---
        sendBroadcastButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 创建一个带有自定义 Action 的 Intent
                Intent customBroadcastIntent = new Intent(CUSTOM_SMS_RECEIVED_ACTION);
                // 发送无序广播 (Normal Broadcast)
                sendBroadcast(customBroadcastIntent);
            }
        });
        // --- 监听器设置结束 ---
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 注销广播接收者以防止内存泄漏
        if (customReceiver != null) {
            unregisterReceiver(customReceiver);
            customReceiver = null; // 帮助垃圾回收
        }
    }
}
